package com.qf.mvc02.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

@Controller
@RequestMapping("/down")
public class DownLoadController {

    @Value("${mvc.fileDir}")
    private String fileDir;


    @RequestMapping("/test1")
    public void test1(String name, HttpServletResponse response) throws IOException {

        //1 根据文件名找到服务器端对应的文件
        final String filepath = fileDir + name;

        // 添加响应头，让浏览器以附件的形式下载文件
        response.setHeader("content-disposition","attachment;filename=aaa.jpg");

        //2 把文件的内容写入 response对象的输出流中
        IOUtils.copy(new FileInputStream(filepath),response.getOutputStream());
    }

}
